home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / cvt100.zip / FILEIO.C < prev    next >
Text File  |  1988-08-02  |  11KB  |  316 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5.  
  6. #define LOGBUFFSIZE 1024
  7.  
  8. /**************************************************************************/
  9. /* Function prototypes                                                    */
  10.  
  11. void Fileinit( void );            /* Initialize the file system */
  12. void SaveSetup( void );           /* Save setup information to file */
  13. int  GetTTSetup( void );          /* Set Communications setup values */
  14. int  GetVidSetup( void );         /* Set Video setup values */
  15.  
  16. void OpenLogFile( void );         /* Open the logfile */
  17. void WriteLog( char );            /* Write a character to the log file */
  18. void FlushLogBuff( void );        /* Flush the log file buffer to disk */
  19. void CloseLogFile( void );        /* Close the log file */
  20.  
  21.  
  22. /**************************************************************************/
  23. /* Global Variables                                                       */
  24.  
  25. char setupfile[] = "CVT100.SET";  /* Filename of setup file */
  26. char logfile[] = "CVT100.LOG";    /* Filename of log file */
  27.  
  28. /**************************************************************************/
  29. /* External Variables                                                     */
  30.  
  31. extern unsigned int port;         /* COM port */
  32. extern unsigned int speed;        /* BAUD rate */
  33. extern char parity[5];            /* Parity setting */
  34. extern unsigned int databits;     /* Number of Data bits */
  35. extern unsigned int stopbits;     /* Number of Stop bits */
  36.  
  37. extern unsigned char retracemode; /* Video snow retrace wait mode flag */
  38. extern unsigned char forecolor;   /* Default foreground color */
  39. extern unsigned char backcolor;   /* Default background color */
  40.  
  41. extern unsigned char backspace;   /* Backspace interpretation flag */
  42. extern unsigned char keyclick;    /* Keyclick on/off flag */
  43. extern unsigned char applkeypad;  /* Application key pad mode flag */
  44.  
  45. extern unsigned originmode;       /* Origin mode, relative or absolute */
  46. extern unsigned insertmode;       /* Insert mode, off or on */
  47. extern unsigned autowrap;         /* Automatic wrap mode, off or on */
  48. extern unsigned newline;          /* Newline mode, off or on,  GLOBAL data!*/
  49. extern unsigned cursorvisible;    /* Cursor visibility, on or hidden */
  50. extern unsigned reversebackground;/* Reverse background attribute, on or off*/
  51. extern unsigned screenwid;        /* Screen column width */
  52. extern unsigned char log;         /* Flag to indicate status of Log */
  53.  
  54. /**************************************************************************/
  55. /* Local Static Data                                                      */
  56.  
  57. static char initfilefound = 0;    /* Flag to indication Setup File found */
  58. static int loghandle;             /* Handle of Log file */
  59. static unsigned char logfileopen = 0; /* Flag to indicate log file is open */
  60.  
  61. static char logbuff[LOGBUFFSIZE]; /* Log file buffer */
  62. static int logcount;              /* Count of characters written to logfile*/
  63.  
  64. static struct com_struct {        /* Structure to hold com parameters */
  65.     int port;
  66.     unsigned speed;
  67.     char parity[5];
  68.     int databits;
  69.     int stopbits;
  70. } com;
  71.  
  72. static struct vid_struct {        /* Structure to hold video parameters */
  73.     unsigned char retracemode;
  74.     unsigned char forecolor;
  75.     unsigned char backcolor;
  76. } vid;
  77.  
  78. static struct key_struct {        /* Structure to hold keyboard parameters */
  79.     unsigned char keyclick;
  80.     unsigned char backspace;
  81.     unsigned char applkeypad;
  82. } key;
  83.  
  84. static struct emul_struct {       /* Structure to hold emulation parameters */
  85.     unsigned int originmode;
  86.     unsigned int insertmode;
  87.     unsigned int autowrap;
  88.     unsigned int newline;
  89.     unsigned int cursorvisible;
  90.     unsigned int reversebackground;
  91.     unsigned int screenwid;
  92.     unsigned char log;
  93. } emul;
  94.  
  95.  
  96. static struct setup_save_struct { /* Setup File block definition */
  97.     char crecord[128];               /* space for communications record */
  98.     char vrecord[128];               /* space for video record */
  99.     char krecord[128];               /* space for keyboard record */
  100.     char erecord[128];               /* space for emulation record */
  101.     } setup;
  102.  
  103.  
  104. /**************************************************************************/
  105. /**************************************************************************/
  106.  
  107.  
  108. /* F I L E I N I T -- initialize the file system */
  109.  
  110. void Fileinit() {
  111.     int handle;
  112.     int bytes;
  113.                                   /* Try and open setup file for input */
  114.     if ( (handle = _open(setupfile,O_RDONLY)) == -1)
  115.         return;                      /* If failed then just return */
  116.  
  117.                                   /* Try and read a setup block from file */
  118.     bytes = _read(handle,&setup,sizeof(setup));
  119.     if (bytes < 0)                   /* If failed then this is a fatal error */
  120.        badexit("IO error reading Setup file");
  121.     else if (bytes != sizeof(setup))
  122.        badexit("Number of bytes read error on Setup file");
  123.     if (_close(handle) != 0)      /* Close the setup file */
  124.        badexit("File Close error on Setup file");
  125.  
  126.     initfilefound = 1;            /* Set setup information read flag */
  127.  
  128.                                   /* Move saved information to parm struct's*/
  129.     memcpy(&com,setup.crecord,sizeof(com));
  130.     memcpy(&vid,setup.vrecord,sizeof(vid));
  131.     memcpy(&key,setup.krecord,sizeof(key));
  132.     memcpy(&emul,setup.erecord,sizeof(emul));
  133.  
  134. }
  135.  
  136.  
  137. /* S A V E S E T U P -- save the setup information */
  138.  
  139. void SaveSetup() {
  140.     int handle;
  141.     int bytes;
  142.  
  143.  
  144.     com.port = port;              /* Save communications parameters */
  145.     com.speed = speed;
  146.     memcpy(com.parity,parity,sizeof(com.parity));
  147.     com.databits = databits;
  148.     com.stopbits = stopbits;
  149.  
  150.     vid.retracemode = retracemode;/* Save video parameters */
  151.     vid.forecolor = forecolor;
  152.     vid.backcolor = backcolor;
  153.  
  154.     key.backspace = backspace;    /* Save KeyBoard parameters */
  155.     key.keyclick = keyclick;
  156.     key.applkeypad = applkeypad;
  157.  
  158.     emul.originmode = originmode; /* Save VT emulation values */
  159.     emul.insertmode = insertmode;
  160.     emul.autowrap = autowrap;
  161.     emul.newline = newline;
  162.     emul.cursorvisible = cursorvisible;
  163.     emul.reversebackground = reversebackground;
  164.     emul.screenwid = screenwid;
  165.     emul.log = log;
  166.  
  167.                                   /* Initialize setup block to zeros */
  168.     memset(&setup,'\0',sizeof(setup));
  169.                                   /* Move parm information to setup block */
  170.     memcpy(setup.crecord,&com,sizeof(com));
  171.     memcpy(setup.vrecord,&vid,sizeof(vid));
  172.     memcpy(setup.krecord,&key,sizeof(key));
  173.     memcpy(setup.erecord,&emul,sizeof(emul));
  174.  
  175.                                   /* Try and create a setup file for writing */
  176.     if ( (handle = _creat(setupfile,0)) == -1)
  177.         badexit("IO error Opening setup file for output");
  178.  
  179.  
  180.                                   /* Try and write the setup block to disk*/
  181.     bytes = _write(handle,&setup,sizeof(setup));
  182.     if (bytes != sizeof(setup))
  183.        badexit("Number of bytes write error on Setup file");
  184.     if (_close(handle) != 0)      /* Try and close the output file */
  185.        badexit("File Close error on Setup file");
  186.  
  187.     initfilefound = 1;            /* Set flag indicating setup is good */
  188.  
  189. }
  190.  
  191.  
  192.  
  193.  
  194. /* G E T T T S E T U P -- Get the communications setup saved in a file */
  195.  
  196. int GetTTSetup( void ) {
  197.  
  198.     if (initfilefound == 0)       /* If no setup file was found */
  199.         return(0);                   /* Then return 0 for no action taken */
  200.  
  201.     port = com.port;              /* Set communications parameters */
  202.     speed = com.speed;            /* to saved values */
  203.     memcpy(parity,com.parity,sizeof(com.parity));
  204.     databits = com.databits;
  205.     stopbits = com.stopbits;
  206.     return(1);                    /* Return 1 for action taken */
  207. }
  208.  
  209. /* G E T V I D S E T U P -- Get the video setup saved in file */
  210.  
  211. int GetVidSetup( void ) {
  212.  
  213.     if (initfilefound == 0)       /* If no setup file was found *